<template>
  <view>
    <div class="search-box">
      <my-search></my-search>
    </div>
    <!-- 1.轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item,i) in swipperList" :key="i">
        <navigator class="swiper-item" :url="'/subpkg/good_detail/good_detail?goods_id=' + item.goods_id">
          <image :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>

    </swiper>
    <!-- 2.分类导航区域 -->
    <view class="cate-list">
      <view class="cate-item" v-for="(item,i) in navList" @click="navHandle(item)" :key="i">
        <image :src="item.image_src" mode=""></image>
      </view>
    </view>

    <!-- 3.楼层区域 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item,i) in floorList" :key="i">
         <!-- 3.1 楼层标题 -->
        <image :src="item.floor_title.image_src" mode="" class="floor-title"></image>
        <!-- 3.2 楼层图片 -->
        <view class="floor-img-box">
          <!-- 3.2.1 左侧大图片 -->
          <navigator class="floor-left-img" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src"
                   :style="{width: item.product_list[0].image_width + 'rpx'}"
                   mode="widthFix"></image>
          </navigator>
          <!-- 3.2.2 右侧图片列表 -->
          <view class="floor-right-img">
            <navigator class="floor-item" 
                  v-for="(item,index) in item.product_list" 
                  v-if="index !==0"
                  :url="item.url"
                  :key="index">
              <image :src="item.image_src" 
                     :style="{width: item.image_width + 'rpx'}"
                     mode="widthFix"></image>
           </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>

</template>

<script>
  import mixBadge from '@/mixins/tabbar-badge.js'
  export default {
    mixins:[mixBadge],
    data() {
      return {
        // 轮播图数据，默认空数组
        swipperList: [],
        // 分类导航
        navList: [],
        // 楼层数据
        floorList: []
      };
    },
    onLoad() {
      // 调用获取轮播图
      this.getSwipperList();
      // 调用分类导航
      this.getNavList();
      // 调用楼层
      this.getFloorList();
    },
    methods: {
      async getFloorList() {
        const res = await uni.$http.get('/api/public/v1/home/floordata');
        if (res.data.meta.status !== 200) return uni.$showMsg();
        
        // 处理数据 
        res.data.message.forEach(item=>{
             item.product_list.forEach(prod=>{
                 prod.url = '/subpkg/shop_list/shop_list?' + prod.navigator_url.split("?")[1];
             })
        })
        this.floorList = res.data.message;
      },
      // 跳转到分类页面
      navHandle(item) {
        // console.log(item)
        if (item.name == '分类') {
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      async getNavList() {
        const res = await uni.$http.get('/api/public/v1/home/catitems');
        // 数据返回失败
        if (res.data.meta.status !== 200) return uni.$showMsg();
        // 设置分类导航数据
        this.navList = res.data.message;
      },
      async getSwipperList() {
        const res = await uni.$http.get('/api/public/v1/home/swiperdata');
        console.log(res.data.message);
        // 判断接口是否正常返回数据
        if (res.data.meta.status !== 200) {
          // return uni.showToast({
          //   title:'数据加载失败!',
          //   duration:1500,
          //   icon:"none"
          // })
          return uni.$showMsg();
        }

        this.swipperList = res.data.message;
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }

  }

  // 分类导航
  .cate-list {
    display: flex;
    justify-content: space-around;
    margin-top: 10rpx;

    image {
      width: 128rpx;
      height: 140rpx;
    }
  }

  // 楼层区域
  .floor-list {
    margin-top: 10rpx;

    .floor-title {
      height: 60rpx;
    }
    // 楼层图片
    .floor-img-box {
      padding-left: 10rpx;
      display: flex;
      // 右侧图片
      .floor-right-img {
          display: flex;
          flex-wrap: wrap;
          justify-content: space-around;
      }
    }
  }
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }
</style>
